package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.entity.TruckOrder;
import com.example.springboot.entity.vo.OrderPageVO;
import com.example.springboot.service.UserOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: jyf2000jyf
 * @Date: 2022/5/12
 * @Time: 8:59
 * @Email: 904593011@qq.com
 * @Description:
 **/
@RestController
@RequestMapping("user/order")
public class UserOrderController {

    @Autowired
    UserOrderService userOrderService;

    // 新增和修改
    @PostMapping
    public Result save(@RequestBody TruckOrder order) {
        // 新增或者更新
        return Result.success(userOrderService.save(order));
    }

    // 查询所有数据
    @GetMapping
    public Result index(@RequestParam String username) {
        List<TruckOrder> all = userOrderService.findAll(username);
        return Result.success(all);
    }

    @GetMapping("delete")
    public Result delete(@RequestParam Integer id) {
        userOrderService.delete(id);
        return Result.success(true);
    }

    @PostMapping("delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        for (Integer id : ids) {
            userOrderService.delete(id);
        }
        return Result.success(true);
    }

    @GetMapping("page")
    public Result indexByPage(@RequestParam Integer pageNum,
                              @RequestParam Integer pageSize,
                              @RequestParam String company,
                              @RequestParam String status,
                              @RequestParam String username) {
        pageNum = (pageNum - 1) * pageSize;
        List<TruckOrder> data;
        Integer total;
        if (status == null || "".equals(status)) {
            data = userOrderService.findAll(pageNum, pageSize, company, -1, username);
            total = userOrderService.getAmount(company, -1, username);
        } else {
            data = userOrderService.findAll(pageNum, pageSize, company, Integer.valueOf(status),username);
            total = userOrderService.getAmount(company, Integer.valueOf(status),username);
        }
        return Result.success(new OrderPageVO(data, total));
    }
}
